﻿using App.Desk.CameraHelper.HK;
using App.Model;
using App.Untiys;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Win.Dal;

namespace App.Desk
{
    public class GlobalSampling
    {
        /// <summary>
        /// 海康摄像头SDK
        /// </summary>
        public static HK_IPCamera Q_HKCameraHelper;

        /// <summary>
        /// 向左采样 102，向右采样101，自动采样120
        /// </summary>
        public static int SamplingKey = 120;

        /// <summary>
        /// 是否点击了自动采样按钮 开启自动采样
        /// </summary>
        public static bool SamplingAutoBegining = false;

        /// <summary>
        /// 当前采样状态 采样中  为空  采样终止
        /// </summary>
        //public static string SamplingStatus = "";

        /// <summary>
        /// 当前采样的本地批次码
        /// </summary>
        public static int LocalBatchId = -1;

        /// <summary>
        /// 采样是否准备好
        /// </summary>
        public static bool SamplingIsReady = false;

        /// <summary>
        /// 下一节触发
        /// </summary>
        public static bool AllowContinue = false;

        /// <summary>
        /// 允许采样
        /// </summary>
        public static bool AllowSampling = false;

        /// <summary>
        /// 大小车去目标点中
        /// </summary>
        public static bool BAndSCarToTarget = false;

        /// <summary>
        /// 当前采样数据
        /// </summary>
        public static ProSampling CurrentSamplingData;

        public static bool IsSampling { get; set; } = false;

        /// <summary>
        /// 截图信号重复获取规避状态 
        /// </summary>
        public static bool IsGetScreenshot { get; set; } = false;

        /// <summary>
        /// 是否获取到下一节触发信号 规避状态 
        /// </summary>
        public static bool IsGetContinue { get; set; } = false;

        /// <summary>
        /// 是否已经获取过一次采样完成信号 规避状态 
        /// </summary>
        public static bool IsGetComplete { get; set; } = false;


        /// <summary>
        /// 重置所有状态
        /// </summary>
        public static void ResetStatus()
        {
            //SamplingIsReady = false;
            AllowContinue = false;
            LocalBatchId = -1;
            CurrentSamplingData = null;
            IsSampling = false;
            SamplingAutoBegining = false;
            AllowSampling = false;
            IsGetComplete = false;
            IsGetScreenshot = false;
            CurrentSamplingData = new ProSampling();
        }

        public static void ResetIsGetStatus()
        {
            IsGetComplete = false;
            IsGetScreenshot = false;
        }

        /// <summary>
        /// 采完完成 抓拍图片
        /// </summary>
        /// <returns></returns> 
        public static string Screenshot(ProSampling proSampling)
        {
            try
            {
                if(GlobalSampling.Q_HKCameraHelper==null)
                {
                    LogHelper.DoWriteLogType(Untiys.LogType.Sampling, $"摄像头未初始化");
                    return $"摄像头未初始化";
                }
                string picPath = Environment.CurrentDirectory.ToString() + "\\Screenshot\\" + DateTime.Now.ToString("yyyyMMdd");
                if (!(System.IO.Directory.Exists(picPath)))
                    System.IO.Directory.CreateDirectory(picPath);
                picPath = picPath + "\\" + $"{proSampling.SamplingId}.jpg";
                if (!GlobalSampling.Q_HKCameraHelper.SaveJPEG(picPath))
                {
                    LogHelper.DoWriteLogType(Untiys.LogType.Sampling, $"第{proSampling.TrainIndex}节采样图片抓拍失败");
                    return $"第{proSampling.TrainIndex}节采样图片抓拍失败";
                }
                proSampling.UploadPicObj = DataTranf.ImageToBytes(picPath);
                proSampling.UploadPicPath = picPath;
                if (ProSamplingServices.UpdateImagePath(proSampling.SamplingId, picPath))
                {
                    LogHelper.DoWriteLogType(Untiys.LogType.Sampling, $"第{proSampling.TrainIndex}节采样图片保存成功");
                    return $"第{proSampling.TrainIndex}节采样图片保存成功";
                }
                return "OK";
            }
            catch (Exception ex)
            {
                LogHelper.DoWriteLogType(Untiys.LogType.Error, $"处理图片失败:{ex.Message.ToString()}");
                return $"处理图片失败:{ex.Message.ToString()}";
            }
        }

    }
}
